#!/bin/sh
#*********************************************************************
#   Copyright (c) International Business Machines  Corp., 2000
#
#   This program is free software;  you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY;  without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#   the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program;  if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
#  FILE   : ssh06
#
#  PURPOSE: Tests to see that ssh allows different port
#
#  SETUP: The program `/usr/bin/expect' MUST be installed.
#
#  HISTORY:
#    03/03 Jerone Young (jeroney@us.ibm.com)
#
#  Filename:    ssh06
#  Version:     1.0
#  Date:        2013-5-13
#  Author:      Heihuijing
#  Email:       huijing.hei@cs2c.com.cn
#  Summary:     02系统基本功能-04网络服务-10SSH-03基本设置-01端口号修改
#               
#  History:
#               Version 1.0, 2013-5-13
#               - ssh allows port in /etc/ssh/sshd_config, default 22.

#-----------------------------------------------------------------------
# FUNCTION:  do_setup
#-----------------------------------------------------------------------

do_setup()
{
    trap do_cleanup INT EXIT

    serv=sshd
    export REMOTEHOST="localhost"
    export TEST_USER="root"
    export TEST_USER_PASSWD="qwer1234"
    export TEST_USER_HOMEDIR="/$TEST_USER"
    export TEST_PORT="3344"
    TIMESLEEP=${TIMESLEEP:-5}

    #exists expect ssh


    # on server
    # update PasswordAuthentication in conffile
    if [ -r "/etc/ssh/sshd_config" ];then
        SSHD_CONF="/etc/ssh/sshd_config"
    fi

    portline=$(awk '/^#Port/' $SSHD_CONF)
    if [ "x$portline" != "x" ];then
        cp $SSHD_CONF $SSHD_CONF.bak
        sed -i "/^#Port[[:space:]]*22/cPort $TEST_PORT" $SSHD_CONF
        [ $? -eq 0 ] && echo "update $TEST_PORT in $SSHD_CONF success."
    fi

    grep "Port " $SSHD_CONF

    sleep $TIMESLEEP

    restart_daemon ${serv}
    sleep 3
    netstat -ntlp | grep ${serv}
    EchoResult "${serv} restart"
    


}

#-----------------------------------------------------------------------
# FUNCTION:  do_cleanup
#-----------------------------------------------------------------------

function do_cleanup()
{
    mv -f $SSHD_CONF.bak $SSHD_CONF
    restart_daemon ${serv}
    EchoInfo "back $SSHD_CONF to orgin."
}

#-----------------------------------------------------------------------
# FUNCTION:  MAIN
#
# DESCRIPTION: Create Test User
#              Call upon script to make sure an invalid user can not have access
#              Cleanup Test User from system
#              Exit with exit code of script called upon
#-----------------------------------------------------------------------
if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Network_Service
fi

. ${SFROOT}/lib/Echo.sh
. ${CMDLIB}/lib/Net.sh
#. ${CMDLIB}/lib/net_cmdlib.sh


#read_opts $*
do_setup
./ssh_port_s1 
EchoResult "ssh port testcase."
